# coding=utf8
# 删除svn目录
import pexpect
from app import create_app
from app.modules.project.models import ProjectInfo
from app.modules.projectPlan.models import ProjectPlanInfo

application = create_app(flask_config = 'local')

def isCanRun(svn_url):
    aa = raw_input("删除 %s .请输入'111'确认: " % svn_url)
    if aa is not None and aa == '111':
        print "开始删除%s" % svn_url
        return True
    else:
        print "放弃删除%s" % svn_url
        return False


# 删除项目所有工程的svn地址
def removeSvnByPlan(_zentao_id):
    _plan = ProjectPlanInfo.query.filter_by(zt_id = _zentao_id).first()
    if _plan is None:
        print '项目不存在 %s' % _zentao_id
        return

    for _pro in ProjectInfo.query.filter_by(_plan.plan_id).all():
        removeSvnByPro(_pro.project_name)
    pass


def removeSvnByPro(pro_name):
    _info = ProjectInfo.query.filter_by(project_name = pro_name).first()
    if _info is None:
        print "工程%s不存在" % pro_name
        return

    if not isCanRun(_info.vcs_path):
        return
    # 开始删除
    _sh = 'svn　delete　%s -m "beetle删除作废分支"' % _info.vcs_path
    output, status = pexpect.run(_sh, withexitstatus = 1, timeout = 60)
    if status == 0:
        print '删除成功，%s' % _info.vcs_path
    else:
        print '删除异常，%s.cause: %s.' % (_info.vcs_path, output)
    pass

if __name__ == '__main__':
    _zentao_id = '1323'
    with application.app_context():
        # removeSvnByPlan(_zentao_id)
        # removeSvnByPro("pis_2.2.4.救火队0420")
        pass
    pass
